package com.cn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers;
import com.cn.config.PageResult;
import com.cn.damain.dto.ReserveQuery;
import com.cn.damain.po.StAdmin;
import com.cn.damain.po.StTeacher;
import com.cn.damain.po.StUser;
import com.cn.mapper.AdminMapper;
import com.cn.mapper.TeacherMapper;
import com.cn.mapper.UserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * 学生实现类。包括增删改查
 */
@Service
public class UserService extends ServiceImpl<UserMapper, StUser> implements IService<StUser> {

    @Resource
    TeacherMapper teacherMapper;
    @Resource
    AdminMapper adminMapper;

    /**
     * 列出所有学生。可以根据用户名模糊查询
     * @param query
     * @return
     */
    public PageResult<StUser> listData(ReserveQuery query) {
        LambdaQueryChainWrapper<StUser> queryWrapper = lambdaQuery().like(StUser::getName, query.getName());
        if(null != query.getId()) {
            queryWrapper.eq(StUser::getId, query.getId());
        }
        if(StringUtils.isNotBlank(query.getUserName())) {
            queryWrapper.like(StUser::getUserName, query.getUserName());
        }
        if(StringUtils.isNotBlank(query.getStuNo())) {
            queryWrapper.like(StUser::getStuNo, query.getStuNo());
        }
        Page<StUser> page = queryWrapper.page(query.toMpPage());
        return PageResult.of(page,page.getRecords());
    }

    /**
     * 列出所有教师。可以根据用户名模糊查询
     * @param query
     * @return
     */
    public PageResult<StTeacher> listTeacherData(ReserveQuery query) {
        LambdaQueryChainWrapper<StTeacher> queryWrapper = ChainWrappers.lambdaQueryChain(teacherMapper).like(StTeacher::getName, query.getName());
        if(null != query.getId()) {
            queryWrapper.eq(StTeacher::getId, query.getId());
        }
        if(StringUtils.isNotBlank(query.getUserName())) {
            queryWrapper.like(StTeacher::getUserName, query.getUserName());
        }
        if(StringUtils.isNotBlank(query.getStuNo())) {
            queryWrapper.like(StTeacher::getStuNo, query.getStuNo());
        }
        Page<StTeacher> page = queryWrapper.page(query.toMpPage());
        return PageResult.of(page,page.getRecords());
    }

    /**
     * 列出所有管理员。可以根据用户名模糊查询
     * @param query
     * @return
     */
    public PageResult<StAdmin> listAdminData(ReserveQuery query) {
        LambdaQueryChainWrapper<StAdmin> queryWrapper = ChainWrappers.lambdaQueryChain(adminMapper).like(StAdmin::getName, query.getName());
        if(null != query.getId()) {
            queryWrapper.eq(StAdmin::getId, query.getId());
        }
        if(StringUtils.isNotBlank(query.getUserName())) {
            queryWrapper.like(StAdmin::getUserName, query.getUserName());
        }
        Page<StAdmin> page = queryWrapper.page(query.toMpPage());
        return PageResult.of(page,page.getRecords());
    }

    /**
     * 添加一个用户，
     * @param user
     */
    public void saveData(StUser user) throws Exception {
        if(StringUtils.isBlank(user.getPassword())) {
            user.setPassword("123456");
        }
        String userTyp = user.getUserTyp();
        switch (userTyp) {
            case "管理员":
                StAdmin stAdmin = new StAdmin();
                BeanUtil.copyProperties(user, stAdmin, true);
                adminMapper.insert(stAdmin);
                break;
            case "教师":
                StTeacher teacher = new StTeacher();
                BeanUtil.copyProperties(user, teacher, true);
                teacherMapper.insert(teacher);
                break;
            case "学生":
                save(user);
                break;
        }
    }


    /**
     * 用户登录
     * @param user
     */
    public StUser userLogin(StUser user) throws Exception {
        String userTyp = user.getUserTyp();
        String tableName = "";
        switch (userTyp) {
            case "管理员":
                tableName = StAdmin.class.getAnnotation(TableName.class).value();
                break;
            case "教师":
                tableName = StTeacher.class.getAnnotation(TableName.class).value();
                break;
            case "学生":
                tableName = StUser.class.getAnnotation(TableName.class).value();
                break;
        }
        List<StUser> users = this.getBaseMapper().userLogin(user.getUserName(), user.getPassword(), tableName);
        if(users.isEmpty()) {
            return null;
        }
        StUser map = users.get(0);
        map.setUserTyp(userTyp);
        return map;
    }

    /**
     * 编辑用户
     * @param user
     */
    public void editData(StUser user) {
        String userTyp = user.getUserTyp();
        switch (userTyp) {
            case "管理员":
                StAdmin stAdmin = new StAdmin();
                BeanUtil.copyProperties(user, stAdmin, true);
                adminMapper.updateById(stAdmin);
                break;
            case "教师":
                StTeacher teacher = new StTeacher();
                BeanUtil.copyProperties(user, teacher, true);
                teacherMapper.updateById(teacher);
                break;
            case "学生":
                saveOrUpdate(user);
                break;
        }
    }

    /**
     * 删除用户，根据ID
     * @param id
     */
    public void delData(Integer id, String userTyp) {
        switch (userTyp) {
            case "管理员":
                adminMapper.deleteById(id);
                break;
            case "教师":
                teacherMapper.deleteById(id);
                break;
            case "学生":
                removeById(id);
                break;
        }
    }
}
